var express = require('express');
var router = express.Router();
var User = require('../models/User');
var Content = require('../models/Content')

// 统一返回格式
var responseData;

router.use(function (req, res, next) {
    responseData = {
        code: 0,
        messgae: ''
    }
    next()
})

/**
 * 用户注册
 * 注册逻辑
 * 1.用户名不能为空
 * 2.密码不能为空
 * 3.两次输入密码必须一致
 * 
 * 数据库查询：
 *  1.用户是否已经被注册
*/
router.post('/user/register', function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    var repassword = req.body.repassword;
    if (!username) {
        responseData.code = 1;
        responseData.messgae = '用户名不能为空';
        res.json(responseData)
        return
    }
    if (!password) {
        responseData.code = 2;
        responseData.messgae = '密码不能为空';
        res.json(responseData)
        return
    }
    if (password != repassword) {
        responseData.code = 3;
        responseData.messgae = '两次输入密码必须一致';
        res.json(responseData)
        return
    }
    // 用户是否已经被注册，如果存在，需要查库
    User.findOne({
        username: username
    }).then(function (userInfo) {
        if (userInfo) {
            responseData.code = 4;
            responseData.messgae = '用户已经被注册';
            res.json(responseData)
            return
        }
        // 保存信息到数据库
        var user = new User({
            username: username,
            password: password
        });
        return user.save();
    }).then(function (newUserInfo) {
        responseData.messgae = '注册成功';
        res.json(responseData)
    })
})
/**
 * 用户登录
 * 
*/
router.post('/user/login', function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    if (!username || !password) {
        responseData.code = 1;
        responseData.messgae = '用户名或密码不能为空';
        res.json(responseData)
        return
    }
    // 查询数据库中相同用户名和密码的记录是否存在，如果存在则登录成功！
    User.findOne({
        username: username,
        password: password
    }).then(function(userInfo) {
        if (!userInfo) {
            responseData.code = 2;
            responseData.messgae = '用户名或密码错误'
            res.json(responseData);
            return;
        }
        //正确
        responseData.messgae = '登录成功！';
        responseData.userInfo = {
            _id: userInfo._id,
            username: userInfo.username
        }
        req.cookies.set('userInfo', JSON.stringify({
            _id: userInfo._id,
            username: userInfo.username
        }));
        res.json(responseData);
        return;
    })
    
})
/**
 * 退出系统
 * 清除cookies
*/
router.get('/user/logout', function (req, res, next) {
    req.cookies.set('userInfo', null);
    responseData.messgae = '退出成功！';
    res.json(responseData);
    return;
})

/**
 * 获取指定文章的所有评论
*/
router.get('/comment', function(req, res) {
    var contentId = req.query.contentId || '';
    Content.findOne({
        _id: contentId
    }).then(function(con) {
        responseData.data = con.comments;
        res.json(responseData)
    })
})

/**
 * 提交评论
*/
router.post('/comment/post', function(req, res) {
    // 内容的id
    var contentId = req.body.contentId || '';
    var postData = {
        username: req.userInfo.username,
        postTime: new Date(),
        content: req.body.content
    }
    // 查询当前这篇文章的评价
    Content.findOne({
        _id: contentId
    }).then(function(con) {
        con.comments.push(postData);
        return con.save()
    }).then(function(newContent) {
        responseData.messgae = '评论成功！';
        responseData.data = newContent;
        res.json(responseData);
    })
})

module.exports = router